<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>地区查询</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <style>
         :root {
            font-size: 15px;
        }
        
        body {
            padding-top: 15px;
        }
    </style>
</head>

<body>
    <div class="container">
        <form id="editForm" class="row">
            <!-- 输入省份名字 -->
            <div class="mb-3 col">
                <label class="form-label">省份名字</label>
                <input type="text" value="北京" name="province" class="form-control province" placeholder="请输入省份名称">
            </div>
            <div class="mb-3 col">
                <label class="form-label">城市名字</label>
                <input type="text" value="北京" name="city" class="form-control city" placeholder="请输入城市名称">
            </div>
        </form>
        <button type="button" class="btn btn-primary sel-btn">查询</button>
        <br><br>
        <p>地区列表</p>
        <ul class="list-group">
            <!-- 示例地区 -->
            <li class="list-group-item">东城区</li>

        </ul>
    </div>

    <script src="../js/axios.min.js"></script>
    <script>
        //      
        // 获取地区列表: http://hmajax.itheima.net/api/area

        // https://hmajax.itheima.net/api/area
        // 查询参数:
        // pname: 省份或直辖市名字
        // cname: 城市名字

        // 目标:根据省份和城市名宁，查询地区列表
        // 1.查询按钮-点击事件
        document.querySelector('.sel-btn').addEventListener('click', () => {
            // console.log('点击了');
            // 2.获取省份城市名字
            let pName = document.querySelector('.province').value
            let cName = document.querySelector('.city').value

            // 3.基于axios请水地区列表数据
            axios({
                url: 'https://hmajax.itheima.net/api/area',
                params: {
                    pname: pName,
                    cname: cName
                }
            }).then(result => {
                // console.log(result);
                //如输入辽宁省大连市，list: (10) ['中山区', '西岗区', '沙河口区', '甘井子区', '旅顺口区', '金州区', '长海县', '瓦房店市', '普兰店市', '庄河市']message: "获取地区县成功"
                let list = result.data.list
                let theLi = list.map(areaName => `<li class="list-group-item">${areaName}</li>`)
                console.log(theLi);
                // ['<li class="list-group-item">中山区</li>', '<li class="list-group-item">西岗区</li>', '<li class="list-group-item">沙河口区</li>', '<li class="list-group-item">甘井子区</li>', '<li class="list-group-item">旅顺口区</li>', '<li class="list-group-item">金州区</li>', '<li class="list-group-item">长海县</li>', '<li class="list-group-item">瓦房店市</li>', '<li class="list-group-item">普兰店市</li>', '<li class="list-group-item">庄河市</li>']
                let theLin = list.map(areaName => `<li class="list-group-item">${areaName}</li>`).join('')
                console.log(theLin);
                //<li class="list-group-item">中山区</li><li class="list-group-item">西岗区</li><li class="list-group-item">沙河口区</li><li class="list-group-item">甘井子区</li><li class="list-group-item">旅顺口区</li><li class="list-group-item">金州区</li><li class="list-group-item">长海县</li><li class="list-group-item">瓦房店市</li><li class="list-group-item">普兰店市</li><li class="list-group-item">庄河市</li>

                // 4.把数据转li标签插入到页面上
                document.querySelector('.list-group').innerHTML = theLin
            })
        })
    </script>
</body>

</html>